{extend name="public/base" /}
{block name="body"}
<div class="layui-fluid">
    <div class="layui-row">
    	<div class="layui-col-sm12 layui-col-md12 layui-col-lg12">
    		<form id="my-form" class="layui-form" method="post" action="{:url()}">
    			<div class="widget-head">
					<div class="widget-title">商品信息</div>
				</div>
				<div class="widget-body layui-col-lg12">
					<div class="layui-tab" lay-filter="group">
						<ul class="layui-tab-title">
							<li class="layui-this">基本</li>
							<li>规格</li>
							<li>详情</li>
						</ul>
						<div class="layui-tab-content">
							<div class="layui-tab-item layui-show">
								<div class="layui-form-item">
									<label class="layui-form-label form-require">商品名称</label>
									<div class="layui-input-block">
										<input type="text" class="layui-input" name="title" value="{$info.title}" placeholder="请输入商品名称" required>
									</div>
								</div>								
								<div class="layui-form-item">
									<label class="layui-form-label form-require">商品分类</label>
									<div class="layui-input-block">
										<select name="cate">
											<option value="">选择分类</option>
											{volist name="category" id="item"}
                        					<option value="{$item.id}" {if condition="$info['cate'] eq $item.id"}selected="selected"{/if}>{$item.cname|raw}
                        					</option>
                        					{/volist}
										</select>
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label form-require">商品图片</label>
									<div class="layui-input-block">
										<div class="layui-upload">
              								<button type="button" class="layui-btn ajax-photos">图片上传</button> 
              								<blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
              									预览：
                								<div class="layui-upload-list uploader-list">
                									{if isset($info.photo)}
                										{foreach $info.photo as $vo}
                										<div class="file-item">
                											<img src="{$vo}">
                											<input type="hidden" name="photo[]"
																   value="{$vo}">
															<i class="layui-icon layui-icon-close file-item-delete delete-photo"></i>
                										</div>
                										{/foreach}
                									{/if}
                								</div>
             								</blockquote>
            							</div>
									</div>
								</div>								
								<div class="layui-form-item">
									<label class="layui-form-label">关键字</label>
									<div class="layui-input-block">
										<input type="text" class="layui-input" name="keyword" value="{$info.keyword}" placeholder="请输入关键字" required>
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label form-require">商品售价</label>
									<div class="layui-input-inline">
										<input type="text" class="layui-input" name="price" value="{$info.price}" placeholder="请输入商品售价" required>
									</div>
									<div class="layui-form-mid layui-word-aux">市场价</div>
										<div class="layui-input-inline">
										<input type="text" class="layui-input" name="market_price" value="{$info.market_price}" placeholder="请输入市场价" required>
									</div>
									<div class="layui-form-mid layui-word-aux">成本价</div>
										<div class="layui-input-inline">
										<input type="text" class="layui-input" name="cost_price" value="{$info.cost_price|default='0'}" placeholder="请输入成本价" required>
									</div>
								</div>								
								<div class="layui-form-item">
									<label class="layui-form-label form-require">运费模板</label>
									<div class="layui-input-block">
										<select name="shipping_id">
											<option value="0">选择模板</option>
											{volist name="shipping" id="item"}
                        					<option value="{$item.id}" {if condition="$info['shipping_id'] eq $item.id"}selected="selected"{/if}>{$item.name}
                        					</option>
                        					{/volist}
										</select>
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">库存</label>
									<div class="layui-input-inline">
										<input type="text" class="layui-input" name="stock" value="{$info.stock|default='10'}" placeholder="请输入库存" required>
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">销量</label>
									<div class="layui-input-inline">
										<input type="text" class="layui-input" name="sales" value="{$info.sales|default='0'}" placeholder="请输入初始销量" required>
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">状态</label>
									<div class="layui-input-block">
										<input type="radio" name="status" value="1" title="上架" {eq name="info['status']" value="1"}checked{/eq}>
                    					<input type="radio" name="status" value="0" title="下架" {eq name="info['status']" value="0"}checked{/eq}>
									</div>
								</div>
							</div>
							<div class="layui-tab-item">										
								<div class="layui-form-item">
									<label class="layui-form-label">规格分组</label>
									<div class="layui-input-block">
										<select name="type" lay-filter="type">
											<option value="0">选择分组</option>
											{volist name="types" id="item"}
                        					<option value="{$item.id}" {if condition="$info['type'] eq $item.id"}selected="selected"{/if}>{$item.name}
                        					</option>
                        					{/volist}
										</select>
									</div>
								</div>
								<div id="sku" data-item="" data-data=""></div>
							</div>
							<div class="layui-tab-item">
								<div class="layui-form-item">
									<label class="layui-form-label form-require">商品描述</label>
									<div class="layui-input-block">
										<textarea name="content" placeholder="请输入详情内容" id="editor" style="height:400px;">{$info.content}</textarea>
									</div>
								</div>
							</div>
						</div>
						<div class="layui-form-item">
							<div class="layui-input-block">
								<input type="hidden" name="id" value="{$info.id}">
								<button class="layui-btn" lay-submit="*" lay-filter="save">保存</button>
                    			<button class="layui-btn layui-btn-primary" onclick="javascript:history.back(-1);return false;">返回</button>
							</div>						
						</div>
					</div>
				</div>				
    		</form>    		
    	</div>
    </div>
</div>
{/block}
{block name="js"}
<script src="/static/libs/ueditor/ueditor.config.js"></script>
<script src="/static/libs/ueditor/ueditor.all.min.js"></script>
<script src="/static/libs/ueditor/lang/zh-cn/zh-cn.js"></script>
<script>
	layui.config({
        base: '/static/libs/extend/' //静态资源所在路径
    }).extend({
    	sku: 'sku'
    });
    layui.use(['sku','layer','form','jquery'],function(){
		var layer = layui.layer,
        	form  = layui.form,
			$ = layui.jquery,
			sku = layui.sku;
				
		var SKU = null;

		var item = '';
		var data = '';

		{notempty name="sku_data"}
		item = {$spec_item|raw};
		data = {$sku_data|raw};
		SKU = sku.init({ id:'sku', item: item, data:data });
		{/notempty}

		form.on('select(type)', function(data){
			var t = data.value;
			$.ajax({
        		url: '{:url("Goods/getAjaxSpec")}',
        		type: 'get',
        		dataType: 'json',
        		data: {type: t},
        		success: function (result) {
            		if (result.code === 1 ) {
                		item = result.data;
						data = [];
						
						SKU = sku.init({ id:'sku', item: item, data:data });
            		}            		
       			}        		
    		});
    		return false;			
		});

		form.on('submit(save)', function(data) {
			var index = layer.msg('提交中，请稍候', {
            	icon: 16,
            	time: false,
            	shade: 0.3
        	});

			var sku_data = null;
        	var post_data = $(data.form).serialize();
        	if(SKU != null){
        		sku_data = SKU.getData();
        	}
        	post_data = post_data + '&sku=' + JSON.stringify(sku_data);

        	$.ajax({
            	url: data.form.action,
            	type: data.form.method,
            	dataType: 'json',
            	data: post_data,
            	success: function(result) {
                	if (result.code === 1 && result.url != '') {
                		setTimeout(function () {
                    		location.href = result.url;
                		}, 1000);
           	 		}
                	layer.close(index);
            		layer.msg(result.msg);
            	},
            	error: function (xhr, state, errorThrown) {
                	layer.close(index);
                	layer.msg(state + '：' + errorThrown, {icon: 5});
            	}
        	});

        	return false;
		});			
	});
    UE.getEditor('editor',{
        // initialFrameWidth :800,// 设置编辑器宽度
        initialFrameHeight:400,// 设置编辑器高度
        scaleEnabled:true
    });
</script>
{/block}